/**
 * 
 */
package de.wojenko.spe;

import java.io.IOException;

import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import de.wojenko.sps.BacklogItemArtefact;

/**
 * 
 * 
 */
public class CardListPaneController extends AnchorPane {

	@FXML
	private TitledPane title;
	@FXML
	private HBox menu;
	@FXML
	private HBox container;

	private BacklogItemArtefactListController controller;
	
	public CardListPaneController() {
		FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("CardListPane.fxml"));
		fxmlLoader.setRoot(this);
		fxmlLoader.setController(this);

		try {
			fxmlLoader.load();
		} catch (IOException exception) {
			throw new RuntimeException(exception);
		}
	}
	
	public void setListController(BacklogItemArtefactListController controller) {
		this.controller = controller;
		reloadItems();
	}
	
	public void addNewCard() {
		BacklogItemArtefact task = controller.createNewArtefact();
		addNewCard(controller.getName(), task);
	}
	
	public void addNewCard(final String itemName, BacklogItemArtefact task) {
		ObservableList<Node> children = container.getChildren();
		String controllerName = itemName + " / " + String.format("%02d", children.size() + 1);
		children.add(new BacklogItemArtefactController(controllerName, task));
	}
	
	public void setName(String name) {
		title.setText(name);
	}
	
	public String getName() {
		return title.getText();
	}
	
	public void reloadItems() {
		container.getChildren().clear();
		for (BacklogItemArtefact card : controller.getArtefacts()) {
			addNewCard(controller.getName(), card);
		}
	}
}
